Ext.define('wmf.lib.FormPanel', {
	extend: 'Ext.form.Panel',
	constructor: function(config){
		this.callParent(arguments);
    },
	alias: 'widget.wmfForm',
	requires: [
	],
	monitorValid: true,
	paramsAsHash: true,
	optype: '',
	opid: '',

	initComponent:function(){ 
		if(this.allowBlank!==undefined && !this.allowBlank){
			if(this.fieldLabel){ 
				this.fieldLabel += 'color:red">*';
			} 
		} 
		this.callParent(arguments);
	},
	
	layout: {
		type: 'vbox',
		padding: 20,
		align: 'stretch'
	},

	autoScroll: false,
	
	items: [
	],
	
	getInputParams: function() {
		var me = this;
		var baseForm = me.getForm();
		return baseForm.getValues();
	},
	
	submited: function(obj) { //{successFun, FailFn}
		var me = this;
		if(!me.setGrid()) return;
		if(!me.setTree()) return;
		var baseForm = me.getForm();
		var postData = [];
		
		var formvalues = baseForm.getValues();
		var params = { contents:[formvalues],  pos: {isAfter:'on', id:''} };
		if(me.optype != '') params.type = me.optype;
		if(me.opid != '') params.id = me.opid;
		
		postData.push(params);
		
		var api = baseForm.api.submit;
		var prefix = "npsos.direct";
		var suffix = api.substring(prefix.length+1);
		var module = suffix.substring(0, suffix.indexOf("."));
		var method = suffix.substring(suffix.indexOf(".")+1);
		Ext.Ajax.request({
            url: 'router.php',
            method: 'POST',
            waitTitle: 'Connecting',
            waitMsg: 'Sending data...',
            timeout: 3000,
            jsonData: {
            	action: module,
            	method: method,
            	data: postData,
            	type: 'rpc',
            	tid: 999
            },
            success: function(response, opts) {
            	var res = Ext.decode(response.responseText);
            	if(res.result && res.result.success) {
            		if(obj && obj.success) {
            			obj.success.call(me, me, res);
            			if(obj.store) obj.store.reload();
            		} else {
            			Ext.Msg.alert('提示', '操作成功');
            			if(obj.store) obj.store.reload();	
            		}
            	} else {
            		if(obj && obj.failure) {
            			obj.failure.call(me, me, res);
            		} else {
            			Ext.Msg.alert('提示', '操作失败');
            		}
            			
            	}
            	
            },
            failure: function(response, opts) {
            	var res = Ext.decode(response.responseText);
            	if(obj && obj.failure) {
            		obj.failure.call(me, me, res);
            	} else {
            		Ext.Msg.alert('提示', '操作失败');
            	}
            }
       });	
		
	},
	
	setGrid: function() {
    	var me = this;
		var gs = me.query('npeditorgrid');
		for(var i = 0; i < gs.length; i++) {
			var xname = gs[i].name;
			var xvalue = gs[i].getValue();
			if(gs[i].checkEmptyResult == 'empty') {
				Ext.Msg.alert('错误', '请检查列表,某些选项可能为空！');	
				return false;
			}
			if(xname) {
				var fd = me.getForm().findField(xname);
				if(fd) {
					fd.setValue(xvalue);	
				} else {
					var xobj = Ext.create('Ext.form.field.Hidden', {name:xname, value:xvalue});
					me.add(xobj);
					//me.add({xtype:'hiddenField', name:xname, value:xvalue});
				}
			}
		}
		return true;
	},
	
	setTree: function() {
    	var me = this;
		var gs = me.query('npcheckedtree');
		for(var i = 0; i < gs.length; i++) {
			var xname = gs[i].name;
			var xvalue = gs[i].getValue();
			
			if(xname) {
				var fd = me.getForm().findField(xname);
				if(fd) {
					fd.setValue(xvalue);	
				} else {
					var xobj = Ext.create('Ext.form.field.Hidden', {name:xname, value:xvalue});
					me.add(xobj);
					//me.add({xtype:'hiddenField', name:xname, value:xvalue});
				}
			}
		}
		return true;
	}
});
